package net.zigin.nhi.could.waste.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import net.zigin.commons.util.ReflectUtil;
import net.zigin.nhi.could.waste.annotation.LoginWhitePathAnnotation;
import net.zigin.nhi.could.waste.dto.MaterialBaseDto;
import net.zigin.nhi.could.waste.dto.SysUserStaffDto;
import net.zigin.nhi.could.waste.model.MaterialBase;
import net.zigin.nhi.could.waste.service.MaterialBaseService;

import net.zigin.nhi.could.waste.service.MaterialRecordService;
import net.zigin.nhi.could.waste.vo.JsonForResult;
import net.zigin.nhi.could.waste.vo.MaterialBaseVo;
import net.zigin.nhi.could.waste.vo.MaterialRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * MaterialBase控制器类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: MaterialBaseController
 * @date 2021-9-12 21:08:09
 */
@Api(tags = "耗材管理")
@RestController
@RequestMapping("/materialBase")
public class MaterialBaseController extends BaseController {
    @Autowired
    private MaterialBaseService materialBaseService;
    @Autowired
    private MaterialRecordService materialRecordService;

    /**
     * 新增编辑耗材
     *
     * @param materialBaseDto 耗材信息
     * @return 耗材ID
     */
    @ApiOperation("新增编辑耗材")
    @ApiImplicitParam(name = "materialBaseDto", value = "耗材信息", required = true, dataType = "MaterialBaseDto")
    @PostMapping("/save")
    public JsonForResult<String> sava(@RequestBody MaterialBaseDto materialBaseDto) {
        String id = materialBaseService.saveOrUpdate(materialBaseDto);
        return JsonForResult.<String>builder().data(id).success(true).build();
    }

    /**
     * 耗材列表
     *
     * @param materialBaseDto 耗材信息
     * @return 耗材列表
     */
    @ApiOperation("耗材列表")
    @ApiImplicitParam(name = "materialBaseDto", value = "耗材信息", required = true, dataType = "MaterialBaseDto")
    @PostMapping("/getList")
    public JsonForResult<List<MaterialBaseVo>> getList(@RequestBody MaterialBaseDto materialBaseDto) {
        List<MaterialBaseVo> list = materialBaseService.getList(materialBaseDto);
        Integer count = materialBaseService.getCount(materialBaseDto);
        return JsonForResult.<List<MaterialBaseVo>>builder()
                .data(list).total(count)
                .success(true).build();
    }

    /**
     * 耗材详情
     *
     * @param id 耗材ID
     * @return 耗材详情
     */
    @ApiOperation("耗材详情")
    @ApiImplicitParam(name = "id", value = "耗材id", required = true, dataType = "String")
    @GetMapping("/getById/{id}")
    public JsonForResult<MaterialBaseVo> getById(@PathVariable String id) {
        MaterialBase materialBase = materialBaseService.selectByPrimaryKey(id);
        MaterialBaseVo materialBaseVo = ReflectUtil.copyProperties(materialBase, MaterialBaseVo.class);
        return JsonForResult.<MaterialBaseVo>builder().data(materialBaseVo).success(true).build();
    }

    /**
     * 删除耗材
     *
     * @param id 耗材ID
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @ApiOperation("删除耗材")
    @ApiImplicitParam(name = "id", value = "耗材id", required = true, dataType = "String")
    public JsonForResult remove(@PathVariable String id) {
        materialBaseService.remove(id);
        return JsonForResult.builder().success(true).build();
    }

    @DeleteMapping("/judgeRemove/{id}")
    @ApiOperation("判断删除耗材")
    @ApiImplicitParam(name = "id", value = "耗材id", required = true, dataType = "String")
    public JsonForResult<Boolean> judgeRemove(@PathVariable String id) {
        if (materialBaseService.judgeRemove(id)) {
            return JsonForResult.<Boolean>builder().data(false).message("该耗材库存不为空或存在申请记录！").success(true).build();
        }
        return JsonForResult.<Boolean>builder().data(true).success(true).build();
    }

    @ApiOperation("导出耗材列表")
    @ApiImplicitParam(name = "materialBaseDto", value = "耗材信息", required = true, dataType = "MaterialBaseDto")
    @PostMapping("/exportList")
    public void exportList(@RequestBody MaterialBaseDto materialBaseDto, HttpServletResponse response) {
        materialBaseService.exportList(materialBaseDto, response);
    }

    @ApiOperation("导入耗材列表")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "MultipartFile")
    @PostMapping("importList")
    public JsonForResult testImport(@RequestParam("file") MultipartFile file) {
        materialBaseService.importList(file);
        return JsonForResult.builder().success(true).build();
    }

    @LoginWhitePathAnnotation
    @ApiOperation("下载模板")
    @GetMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response) {
        materialBaseService.downloadTemplate(response);
    }

    @ApiOperation("耗材出入库记录")
    @ApiImplicitParam(name = "id", value = "耗材id", required = true, dataType = "String")
    @GetMapping("/getMaterialRecordList/{id}")
    public JsonForResult<List<MaterialRecordVo>> getMaterialRecordList(@PathVariable String id) {
        List<MaterialRecordVo> list = materialRecordService.getInfoListByMaterialBaseId(id);
        return JsonForResult.<List<MaterialRecordVo>>builder().data(list).success(true).build();
    }


}
